#!/bin/bash

#clean the data file
echo \#start >figure2.dat


#run the simulation 10 times
# simulation_red() maxthreshold
simulation_red()
{ 
# $1 is the first parameter of simulation()
n1=3 #minithreshold
n2=$1 #maxthreshold

for simulation_i in  1 2 3 4 5 6 7 8 9 10; do
	# ns red.tcl minithreshold seed maxthreshold
	ns red.tcl $n1 $simulation_i $n2
done
}

#run the simulation
for (( maxthreshold_i=4; maxthreshold_i <=12; maxthreshold_i++ )) 
do
	# simulation_red() maxthreshold
	simulation_red $maxthreshold_i >temp_data

	#store data in figure2.dat by following format
	# maxthreshold delay throughput throughput/delay
	# maxthreshold delay throughput throughput/delay
	# ...
	cat temp_data | awk 'BEGIN{maxthresh=0; delay=0; throughput=0} 
			{if ($3=="maxthresh"){maxthresh=$4}; 	
			if ($2=="delay"){delay=delay+$5};
 			if ($2=="throughput"){throughput=throughput+$5}} 
			END{print maxthresh, delay/10.0, throughput/10.0, throughput/delay }' >> figure2.dat
done

#plot the graph
gnuplot figure2_put.plt
gnuplot figure3_delay.plt
gnuplot figure4_ratio.plt
